import numpy as np
from scipy import optimize
import math


def cclate_D(x):  # x: distance between the line and the western eddge
    return 110 + (2 * 1852 - x) * math.tan(a)


def cclate_m(x):
    D = cclate_D(x)
    return (
        math.sin(np.pi / 3)
        * D
        # / np.sin(np.pi / 6 - math.atan(math.tan(a) * math.sin(b)))
        / np.sin(np.pi / 6 - a)
    )


def cclate_n(x):
    D = cclate_D(x)
    return (
        math.sin(np.pi / 3)
        * D
        # / np.sin(np.pi / 6 + math.atan(math.tan(a) * math.sin(b)))
        / np.sin(np.pi / 6 + a)
    )


def equation_1(x):
    return cclate_m(x) * math.cos(a) - x


def equation_2(x, c):
    return cclate_m(x) * math.cos(a) - x + c


if __name__ == "__main__":
    i = 0
    a = math.radians(1.5)  # alpha
    # a = 1.5 * np.pi / 180  # alpha
    # b = 90 * np.pi / 180  # beta
    solution = optimize.fsolve(equation_1, 0)
    # print(solution)
    x = solution[0]
    m = cclate_m(x)
    n = cclate_n(x)
    c = 0.9 * (m + n) * math.cos(a)  # the beginning of the next belt
    print(x, m, n, c)
    edge = (m + n) * math.cos(a)
    while edge < 4 * 1852:
        i += 1
        # print("c:", c)
        # print("depth:", cclate_D(x))
        solution = optimize.fsolve(equation_2, 0, args=(c,))
        x = solution[0]
        print(solution[0])
        m = cclate_m(x)
        n = cclate_n(x)
        edge = c + (m + n) * math.cos(a)
        c += 0.9 * (m + n) * math.cos(a)
    print("i:", i)
